const url = require("url");
const fs = require("fs");

const outputI18nFilePlugin = require("./plugins/outputI18nFilePlugin");

module.exports = {
  publicPath: "/",
  devServer: {
    host: "m.zhaohu.co",
    port: 8880,
    proxy: {
      "/api": {
        target: "http://127.0.0.1:8880/",
        pathRewrite(path) {
          // 使用云梯的接口组织结构
          path = path
            .replace("/query", "")
            .replace("/save", "")
            .replace("/delete", "")
            .replace("/update", "");
          let u = url.parse(path);
          let p = u.pathname.split("/");
          let n = p[p.length - 1];

          /*
                    if (n == 'api') {
                        return '/static/mock/404.json';
                    }
                    */

          let qs = {};
          for (let query of (u.query || "").split("&")) {
            let q = query.split("=");
            qs[q[0]] = q[1];
          }

          let re_path = `/static/mock/${n}${u.query ? "+" + u.query : ""}.json`;

          if (!fs.existsSync(`${__dirname}/public/${re_path}`)) {
            return "/static/mock/404.json";
          }

          return re_path;
        }
      }
    }
  },

  configureWebpack: {
    plugins: [
      // 需要生成本地化语言文件的语言列表
      new outputI18nFilePlugin(["en-us", "zh-cn"])
    ]
  },

  chainWebpack: () => {
    /*
        config
            .plugin( 'output-i18n' )
            .use(outputI18nFilePlugin)

        config.module
            .rule( 'vue' )
                .test(/\.vue$/)
                    .use('cache-loader')
                    .loader('cache-loader')
                .end()
                    .use( 'vue-loader' )
                    .loader( 'vue-loader' )
                .end()
                    .use('output-i18n')
                    .loader('output-i18n')
                    */
  }
};
